#include "XYControlGroupBox.h"
#include <QDebug>
#include <QFont>
XYControlGroupBox::XYControlGroupBox(QWidget *parent)
    :QGroupBox(parent)
{  
    initContent();
    initConnection();
    initSpeedLevels();
}

void XYControlGroupBox::initSpeedLevels()
{
    QList<double> levels;

    levels << 0.01;
    levels << 0.03;
    levels << 0.1;
    levels << 0.3;
    levels << 1;
    levels << 3;
    levels << 10;
    levels << 30;
    levels << 100;

    setupSpeedLevels(levels);
}

void XYControlGroupBox::setupSpeedLevels(QList<double> levels)
{
    if(levels.isEmpty())
    {
        return;
    }
    m_levels = levels;
    int num = m_levels.size();
    verticalSliderSpeedXY->setMaximum(num - 1);
    verticalSliderSpeedXY->setValue(num / 2);
    //setSpeedLevel(num / 2);
}

void XYControlGroupBox::initContent()
{
    verticalLayout = new QVBoxLayout;
    verticalLayout->setSpacing(0);
    verticalSliderSpeedXY = new QSlider(this);
//    QSizePolicy sizePolicy2(QSizePolicy::Fixed, QSizePolicy::Expanding);
//    sizePolicy2.setHorizontalStretch(0);
//    sizePolicy2.setVerticalStretch(0);
//    sizePolicy2.setHeightForWidth(verticalSliderSpeedXY->sizePolicy().hasHeightForWidth());
//    verticalSliderSpeedXY->setSizePolicy(sizePolicy2);
    verticalSliderSpeedXY->setMinimumSize(QSize(20, 0));
    verticalSliderSpeedXY->setMaximum(9);
    verticalSliderSpeedXY->setPageStep(1);
    verticalSliderSpeedXY->setOrientation(Qt::Vertical);
    verticalLayout->addWidget(verticalSliderSpeedXY);

    labelSpeedXY = new QLabel(this);
    verticalLayout->addWidget(labelSpeedXY);

    horizontalLayout = new QHBoxLayout(this);
    horizontalLayout->setSpacing(0);
    m_xyControl = new XYControlWidget(this);
    horizontalLayout->addWidget(m_xyControl);
    horizontalLayout->addLayout(verticalLayout);

    setLayout(horizontalLayout);

    setTitle("XY Move Control");
}

void XYControlGroupBox::speedUp()
{
    qDebug() << "XYControlGroupBox::speedUp()";
    int max = m_levels.size() - 1;
    int v = verticalSliderSpeedXY->value();
    int v_new = qBound(0, v + 1, max);
    if(v != v_new)
    {
        verticalSliderSpeedXY->setValue(v_new);
    }
}

void XYControlGroupBox::speedDown()
{
    qDebug() << "XYControlGroupBox::speedDown()";
    int max = m_levels.size() - 1;
    int v = verticalSliderSpeedXY->value();
    int v_new = qBound(0, v - 1, max);
    if(v != v_new)
    {
        verticalSliderSpeedXY->setValue(v_new);
    }
}

void XYControlGroupBox::on_moveForward()
{
    double spd = speed();
    emit moveForward(spd);
}

void XYControlGroupBox::on_moveBackward()
{
    double spd = speed();
    emit moveBackward(spd);
}

void XYControlGroupBox::on_moveLeft()
{
    double spd = speed();
    emit moveLeft(spd);
}

void XYControlGroupBox::on_moveRight()
{
    double spd = speed();
    emit moveRight(spd);
}

void XYControlGroupBox::initConnection()
{
    connect(m_xyControl, SIGNAL(moveForwardStop()), this, SIGNAL(moveForwardStop()));
    connect(m_xyControl, SIGNAL(moveBackwardStop()), this, SIGNAL(moveBackwardStop()));
    connect(m_xyControl, SIGNAL(moveLeftStop()), this, SIGNAL(moveLeftStop()));
    connect(m_xyControl, SIGNAL(moveRightStop()), this, SIGNAL(moveRightStop()));
    connect(m_xyControl, SIGNAL(stop()), this, SIGNAL(stop()));

    connect(m_xyControl, SIGNAL(moveForward()), this, SLOT(on_moveForward()));
    connect(m_xyControl, SIGNAL(moveBackward()), this, SLOT(on_moveBackward()));
    connect(m_xyControl, SIGNAL(moveLeft()), this, SLOT(on_moveLeft()));
    connect(m_xyControl, SIGNAL(moveRight()), this, SLOT(on_moveRight()));

    connect(verticalSliderSpeedXY, SIGNAL(valueChanged(int)), this, SLOT(on_sliderValueChanged(int)));
}

void XYControlGroupBox::on_sliderValueChanged(int n)
{
    double speed = m_levels.at(n);
    labelSpeedXY->setText(QString("%1\n  mm/s").arg(speed, 6, 'f', 2, ' '));
    qDebug() <<"setSpeedLevel n =" << n << ",Speed = " << speed;
    emit changeSpeed(speed);
}

double XYControlGroupBox::speed()
{
    int v = verticalSliderSpeedXY->value();
    double spd = m_levels.at(v);
    return spd;
}
